/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jiframeTiposCambioDiario.java
 *
 * Created on 10-dic-2010, 8:56:23
 */

package TiposCambio;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import suministros.ejb30.TO.AnoTO;
import suministros.ejb30.TO.MesTO;
import suministros.ejb30.entity.TipoCambioDiario;
/**
 *
 * @author Luis_Angel
 */
public class jiframeTiposCambioDiario extends javax.swing.JDialog {

    private List<TipoCambioDiario> lista;
    private JTableBinding jTableBinding;
    /** Creates new form jiframeTiposCambioDiario */
    public jiframeTiposCambioDiario(java.awt.Frame parent, boolean modal) {
       super(parent, modal);
        initComponents();
        try {
            Listas.cargarAnosMesesCambios();
        } catch (Exception ex) {
            Logger.getLogger(jiframeTiposCambioDiario.class.getName()).log(Level.SEVERE, null, ex);
        }
         llenarComboAno();
       // cargaTabla();
    }


      public void cargaTabla(){
        this.bindingGroup = new BindingGroup();
                try {
                    String anio = ((AnoTO)this.jcboAnio.getSelectedItem()).getAno();
                    String mes = ((MesTO)this.jcboMes.getSelectedItem()).getMes();
                    lista = GestionPedidosDelegate.getInstance().getTipoCambioDiario(anio, mes);
                } catch (Exception ex) {
                   ex.printStackTrace();
                }
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ,lista , this.jtCambio);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding0.setColumnName("Fecha");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding0_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${tipoCambio}"));

            columnBinding0_1.setColumnName("Cambio");
            columnBinding0_1.setColumnClass(BigDecimal.class);
            columnBinding0_1.setEditable(false);

           

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBeanBindingMes1 = new administracion.BeanBinding.CboBeanBindingMes();
        cboBeanBindingAno1 = new administracion.BeanBinding.CboBeanBindingAno();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtCambio = new javax.swing.JTable();
        jbtnActualizar = new javax.swing.JButton();
        jbtnSalir = new javax.swing.JButton();
        jbEditar1 = new javax.swing.JButton();
        jcboAnio = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        Mes = new javax.swing.JLabel();
        jcboMes = new javax.swing.JComboBox();

        setTitle("Administrar Tipos de Cambio Diario");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jtCambio.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fecha", "Cambio"
            }
        ));
        jScrollPane1.setViewportView(jtCambio);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
        );

        jbtnActualizar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/edit.png"))); // NOI18N
        jbtnActualizar.setText("Modificar");
        jbtnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnActualizarActionPerformed(evt);
            }
        });

        jbtnSalir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/application_exit.png"))); // NOI18N
        jbtnSalir.setText("Salir");
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(evt);
            }
        });

        jbEditar1.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
        jbEditar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/tab_new_background.png"))); // NOI18N
        jbEditar1.setText("Nuevo");
        jbEditar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEditar1ActionPerformed(evt);
            }
        });

        jcboAnio.setFont(new java.awt.Font("Arial", 0, 12));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingAno1, eLProperty, jcboAnio);
        bindingGroup.addBinding(jComboBoxBinding);

        jcboAnio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcboAnioActionPerformed(evt);
            }
        });
        jcboAnio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcboAnioKeyPressed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setText("Año");

        Mes.setFont(new java.awt.Font("Arial", 1, 12));
        Mes.setText("Mes");

        jcboMes.setFont(new java.awt.Font("Arial", 0, 12));

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingMes1, eLProperty, jcboMes);
        bindingGroup.addBinding(jComboBoxBinding);

        jcboMes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcboMesItemStateChanged(evt);
            }
        });
        jcboMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcboMesKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbEditar1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnActualizar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnSalir))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(jLabel2)
                        .addGap(4, 4, 4)
                        .addComponent(jcboAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(Mes)
                        .addGap(4, 4, 4)
                        .addComponent(jcboMes, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbEditar1, jbtnActualizar, jbtnSalir});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(jcboAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(Mes))
                    .addComponent(jcboMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnSalir)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbEditar1))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnActualizarActionPerformed
        int filaElegida = this.jtCambio.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un item!");
            this.jtCambio.requestFocus();
            return;
        }

        try {
            TipoCambioDiario tcd = (TipoCambioDiario)this.lista.get(this.jtCambio.convertRowIndexToModel(filaElegida));
            jdAdminTiposCambioDiario admin = new jdAdminTiposCambioDiario(JOptionPane.getFrameForComponent((JDialog)this),false,this);
            admin.setAdmin(tcd);
            admin.setLocationRelativeTo(this);
            admin.setVisible(true);
           
        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo Cargar el archivo de tipos de cambio\n" + e.getMessage());
        }
        try {

        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("Error al cargar A�os");
        }

}//GEN-LAST:event_jbtnActualizarActionPerformed

    private void llenarComboAno()
      {
        List listaAnos = null;
        List listaAnos_cbo = this.cboBeanBindingAno1.getLista();
        listaAnos_cbo.clear();
        try
        {
          listaAnos = Listas.getAnosCambio();
          this.cboBeanBindingAno1.setLista(listaAnos);
          this.bindingGroup.bind();
          this.jcboAnio.updateUI();
          this.jcboAnio.setSelectedIndex(0);
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener a�os\n" + ex.getMessage());
          ex.printStackTrace();

        }
      }

  private void llenarComboMes() {
    List listaMeses = null;
    List listaMeses_cbo = this.cboBeanBindingMes1.getLista();
    listaMeses_cbo.clear();
    try {
      if (this.jcboAnio.getSelectedItem() != null) {
        AnoTO an = (AnoTO)this.jcboAnio.getSelectedItem();
        listaMeses = Listas.getMesesAnoCambios(an);
        this.cboBeanBindingMes1.setLista(listaMeses);
        this.bindingGroup.bind();
        this.jcboMes.updateUI();
        this.jcboMes.setSelectedIndex(0);
      }
    } catch (Exception ex) {
      Utiles_MensajesDialogo.mensajeAlerta("Error al obtener meses\n" + ex.getMessage());
      ex.printStackTrace();

    }
  }
  
    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jbtnSalirActionPerformed

    private void jbEditar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEditar1ActionPerformed

     jdAdminTiposCambioDiario admin = new jdAdminTiposCambioDiario(null,false,this);
     admin.setLocationRelativeTo(this);
     admin.setTitle("Administrar Tipos de Cambio");
     admin.setVisible(true);
}//GEN-LAST:event_jbEditar1ActionPerformed

    private void jcboAnioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcboAnioActionPerformed
        llenarComboMes();
}//GEN-LAST:event_jcboAnioActionPerformed

    private void jcboAnioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcboAnioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jcboMes.requestFocus();
        }
}//GEN-LAST:event_jcboAnioKeyPressed

    private void jcboMesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcboMesKeyPressed

      /*  if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbBuscar.requestFocus();
        }*/
}//GEN-LAST:event_jcboMesKeyPressed

    private void jcboMesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcboMesItemStateChanged
         cargaTabla();
    }//GEN-LAST:event_jcboMesItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Mes;
    private administracion.BeanBinding.CboBeanBindingAno cboBeanBindingAno1;
    private administracion.BeanBinding.CboBeanBindingMes cboBeanBindingMes1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbEditar1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JComboBox jcboAnio;
    private javax.swing.JComboBox jcboMes;
    private javax.swing.JTable jtCambio;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
